﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Dynamic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Text;

/// <summary>
/// WebAPI访问
/// </summary>
namespace MDK.Common
{
    public static class WebAPIHelper
    {
        /// <summary>
        /// API地址
        /// </summary>
        //public static string staticurl = "http://localhost:3389/API/";
        public static string staticurl = "http://192.168.2.41:8080/API/";

        /// <summary>
        /// 查询 -返回一条
        /// </summary>
        public static T Get<T>(string url)
        {
            Uri uri = new Uri(staticurl + url);
            using (WebClient client = new WebClient())
            {
                client.Headers["Type"] = "GET";
                client.Headers["Accept"] = "application/json";
                client.Encoding = Encoding.UTF8;
                string str = client.DownloadString(uri);
                return JsonConvert.DeserializeObject<T>(str);
            }
        }
        /// <summary>
        /// Post
        /// </summary>
        public static bool Post<T>(string url, T model)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, model);
            ms.Seek(0, SeekOrigin.Begin);
            HttpContent content = new StreamContent(ms);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            return new HttpClient().PostAsync(staticurl + url, content).Result.IsSuccessStatusCode;
        }
    }
}
